package com.khstudio.filebrowser.duallist;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.khstudio.filebrowser.R;
import com.khstudio.filebrowser.duallist.KhDualListLayout.onKhDualListLayoutListener;
import com.khstudio.filebrowser.util.KhDialog;
import com.khstudio.filebrowser.util.KhFile;
import com.khstudio.filebrowser.util.KhLog;
import com.khstudio.filebrowser.util.KhFile.KhFileListener;

public class KhDualListFragment extends Fragment {
    private static final String KEY_PATH = "KhPath";

    private static final int MENU_FILE              = 0x0001;
    private static final int MENU_ADD_FOLDER        = 0x0002;
    private static final int MENU_COPY              = 0x0003;
    private static final int MENU_MOVE              = 0x0004;
    private static final int MENU_DELETE            = 0x0005;
    private static final int MENU_DONE              = 0x0006;
    private static final int MENU_SELECT_ALL_TRUE   = 0x0007;
    private static final int MENU_SELECT_ALL_FALSE  = 0x0008;

    private KhDualListLayout mKhDualListLayout;
    private onKhDualListFragmentListener mListener;

    private MenuItem    mMenuFile;
    private MenuItem    mMenuAddFolder;
    private MenuItem    mMenuCopy;
    private MenuItem    mMenuMove;
    private MenuItem    mMenuDelete;
    private MenuItem    mMenuDone;
    private MenuItem    mMenuSelectAllTrue;
    private MenuItem    mMenuSelectAllFalse;

    private int mMenuSelect;

    KhDialog mDialog;

    public interface onKhDualListFragmentListener {
        void onFolderChange(String path);
        void onFileClick(String path);
    }

    public static KhDualListFragment newInstance(String path) {
        KhDualListFragment f = new KhDualListFragment();

        Bundle args = new Bundle();
        args.putString(KEY_PATH, path);
        f.setArguments(args);

        return f;
    }

    public void setonKhDualListFragmentListener(onKhDualListFragmentListener l) {
        mListener = l;
    }

    public void setPath(String path) {
        mKhDualListLayout.init(path);
    }

    public void back() {
        if(!mKhDualListLayout.modeBack()) {
            mDialog = KhDialog.showDialogSimple(getFragmentManager(), "Exit", "Do you want exit ?");
            mDialog.setPositiveButton(getResources().getString(android.R.string.ok), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().finish();
                }
            });
            mDialog.setCancelButton(getResources().getString(android.R.string.cancel), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                }
            });
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        KhLog.v("KhDualListFragment : onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        KhLog.v("KhDualListFragment : onCreate");

        setHasOptionsMenu(true);
        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
        getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        KhLog.i("KhDualListFragment : onCreateView");
        mListener = null;

        mKhDualListLayout = new KhDualListLayout(getActivity());
        mKhDualListLayout.setonKhDualListLayoutListener(new onKhDualListLayoutListener(){
            @Override
            public void onFolderChange(String path) {
                _invalidateMenu();
                if(mListener != null) mListener.onFolderChange(path);
            }

            @Override
            public void onFileClick(String path) {
                if(mListener != null) mListener.onFileClick(path);
            }

            @Override
            public void onUpdateMenu() {
                _invalidateMenu();
            }
        });

        if (savedInstanceState != null) {
            String path = savedInstanceState.getString(KEY_PATH, null);
            mKhDualListLayout.init(path);
        }

        return mKhDualListLayout;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        KhLog.v("KhDualListFragment : onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        KhLog.v("KhDualListFragment : onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        KhLog.v("KhDualListFragment : onResume");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        KhLog.v("KhDualListFragment : onSaveInstanceState");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        KhLog.v("KhDualListFragment : onConfigurationChanged");
    }

    @Override
    public void onPause() {
        KhLog.v("KhDualListFragment : onPause");
        super.onPause();
    }

    @Override
    public void onStop() {
        KhLog.v("KhDualListFragment : onStop");
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        KhLog.v("KhDualListFragment : onDestroyView");
        super.onDestroyView();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        KhLog.v("KhDualListFragment : onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroy() {
        KhLog.v("KhDualListFragment : onDestroy");
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        KhLog.v("KhDualListFragment : onDetach");
        super.onDetach();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        mMenuFile = menu.add(0, MENU_FILE, MENU_FILE, "File");
        mMenuFile.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mMenuFile.setIcon(R.drawable.kh_duallist_btn_actionbar_file);

        mMenuAddFolder = menu.add(0, MENU_ADD_FOLDER, MENU_ADD_FOLDER, "Add Folder");
        mMenuAddFolder.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mMenuAddFolder.setIcon(R.drawable.kh_duallist_btn_actionbar_add_folder);

        mMenuCopy = menu.add(0, MENU_COPY, MENU_COPY, "Copy");
        mMenuCopy.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mMenuCopy.setIcon(R.drawable.kh_duallist_btn_actionbar_copy);

        mMenuMove = menu.add(0, MENU_MOVE, MENU_MOVE, "Move");
        mMenuMove.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mMenuMove.setIcon(R.drawable.kh_duallist_btn_actionbar_move);

        mMenuDelete = menu.add(0, MENU_DELETE, MENU_DELETE, "Delete");
        mMenuDelete.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mMenuDelete.setIcon(R.drawable.kh_duallist_btn_actionbar_delete);

        mMenuDone = menu.add(0, MENU_DONE, MENU_DONE, "Done");
        mMenuDone.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mMenuDone.setIcon(R.drawable.kh_duallist_btn_actionbar_done);

        mMenuSelectAllTrue = menu.add(0, MENU_SELECT_ALL_TRUE, MENU_SELECT_ALL_TRUE, "Select All");
        mMenuSelectAllTrue.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mMenuSelectAllTrue.setIcon(R.drawable.kh_duallist_btn_actionbar_select_all_true);

        mMenuSelectAllFalse = menu.add(0, MENU_SELECT_ALL_FALSE, MENU_SELECT_ALL_FALSE, "Select All");
        mMenuSelectAllFalse.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mMenuSelectAllFalse.setIcon(R.drawable.kh_duallist_btn_actionbar_select_all_false);

        _invalidateMenu();

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case android.R.id.home:
                back();
            return true;

            case MENU_FILE : {
                KhLog.i("onOptionsItemSelected MENU_FILE");
                mKhDualListLayout.modeSelectFile();
            }
            return true;

            case MENU_ADD_FOLDER : {
                KhLog.i("onOptionsItemSelected MENU_ADD_FOLDER");
                KhFile.createfolder(getActivity(), mKhDualListLayout.getPath(), new KhFileListener() {
                    @Override
                    public void onDone(ArrayList<String> result) {
                        KhLog.i("onOptionsItemSelected MENU_ADD_FOLDER onDone %s",result.get(0));
//                        mKhDualListLayout.addFolder(result.get(0));
                    }
                });
            }
            return true;

            case MENU_COPY : {
                KhLog.i("onOptionsItemSelected MENU_COPY");
                mMenuSelect = MENU_COPY;
                mKhDualListLayout.modeSelectFolder();
            }
            return true;

            case MENU_MOVE : {
                KhLog.i("onOptionsItemSelected MENU_MOVE");
                mMenuSelect = MENU_MOVE;
                mKhDualListLayout.modeSelectFolder();
            }
            return true;

            case MENU_DELETE : {
                KhLog.i("onOptionsItemSelected MENU_DELETE");
                mMenuSelect = MENU_DELETE;
                _createFileActionDialog();
            }
            return true;

            case MENU_DONE : {
                KhLog.i("onOptionsItemSelected MENU_DONE");
                _createFileActionDialog();
            }
            return true;

            case MENU_SELECT_ALL_TRUE : {
                mKhDualListLayout.setFileSelectAll(false);
                _invalidateMenu();
            }
            return true;

            case MENU_SELECT_ALL_FALSE : {
                mKhDualListLayout.setFileSelectAll(true);
                _invalidateMenu();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void _invalidateMenu() {
        int mode = mKhDualListLayout.getMode();

        if(mode == KhDualListLayout.MODE_NORMAL) {
            mMenuFile.setVisible(true);
            mMenuAddFolder.setVisible(false);
            mMenuCopy.setVisible(false);
            mMenuMove.setVisible(false);
            mMenuDelete.setVisible(false);
            mMenuDone.setVisible(false);
            mMenuSelectAllTrue.setVisible(false);
            mMenuSelectAllFalse.setVisible(false);
        } else if(mode == KhDualListLayout.MODE_SELECT_FILE) {
            mMenuFile.setVisible(false);
            mMenuAddFolder.setVisible(true);
            mMenuCopy.setVisible(true);
            mMenuMove.setVisible(true);
            mMenuDelete.setVisible(true);
            mMenuDone.setVisible(false);
            if(mKhDualListLayout.getFileSelectCount() > 0) {
                mMenuCopy.setEnabled(true);
                mMenuMove.setEnabled(true);
                mMenuDelete.setEnabled(true);
            } else {
                mMenuCopy.setEnabled(false);
                mMenuMove.setEnabled(false);
                mMenuDelete.setEnabled(false);
            }

            if(mKhDualListLayout.getFileSelectMax() > 0) {
                if(mKhDualListLayout.isFileSelectAll()) {
                    mMenuSelectAllTrue.setVisible(true);
                    mMenuSelectAllTrue.setEnabled(true);
                    mMenuSelectAllFalse.setVisible(false);
                } else {
                    mMenuSelectAllTrue.setVisible(false);
                    mMenuSelectAllFalse.setVisible(true);
                    mMenuSelectAllFalse.setEnabled(true);
                }
            } else {
                mMenuSelectAllTrue.setVisible(false);
                mMenuSelectAllFalse.setVisible(true);
                mMenuSelectAllFalse.setEnabled(false);
            }
        } else if(mode == KhDualListLayout.MODE_SELECT_FOLDER) {
            mMenuFile.setVisible(false);
            mMenuAddFolder.setVisible(true);
            mMenuCopy.setVisible(false);
            mMenuMove.setVisible(false);
            mMenuDelete.setVisible(false);
            mMenuDone.setVisible(true);
            mMenuDone.setEnabled(true);

            mMenuSelectAllTrue.setVisible(false);
            mMenuSelectAllFalse.setVisible(false);
        }
    }

    private void _createFileActionDialog() {
        int cnt         = mKhDualListLayout.getFileSelectCount();
        String title    = null;
        String msg      = null;

        if(mMenuSelect == MENU_COPY) {
            title = "Copy";
            if(cnt == 1) {
                msg = "Do you want to copy ?";
            } else if(cnt > 1) {
                msg = String.format("Do you want to copy %d files ?",cnt);
            }
        } else if(mMenuSelect == MENU_MOVE) {
            title = "Move";
            if(cnt == 1) {
                msg = "Do you want to move ?";
            } else if(cnt > 1) {
                msg = String.format("Do you want to move %d files ?",cnt);
            }
        } else if(mMenuSelect == MENU_DELETE) {
            title = "Delete";
            if(cnt == 1) {
                msg = "Do you want to delete ?";
            } else if(cnt > 1) {
                msg = String.format("Do you want to delete %d files ?",cnt);
            }
        }

        if(msg != null) {
            mDialog = KhDialog.showDialogSimple(getFragmentManager(), title, msg);
            mDialog.setPositiveButton(getResources().getString(android.R.string.ok), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KhLog.i("_createFileActionDialog onClickPositive");
                    KhFileListener l = new KhFileListener() {
                        @Override
                        public void onDone(ArrayList<String> result) {
                            mKhDualListLayout.modeBase();
                        }
                    };

                    if(mMenuSelect == MENU_COPY) {
                        KhFile.copy(getActivity(), mKhDualListLayout.getFileSelectList(), mKhDualListLayout.getPath(), l);
                    } else if(mMenuSelect == MENU_MOVE) {
                        KhFile.move(getActivity(), mKhDualListLayout.getFileSelectList(), mKhDualListLayout.getPath(), l);
                    } else if(mMenuSelect == MENU_DELETE) {
                        KhFile.delete(getActivity(), mKhDualListLayout.getFileSelectList(), l);
                    }
                    mDialog.dismiss();
                }
            });
            mDialog.setCancelButton(getResources().getString(android.R.string.cancel), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                }
            });
        }
    }
}
